package com.example.houserental.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.houserental.entity.*;
import com.example.houserental.mapper.*;
import com.example.houserental.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <pre>
 *     房屋业务逻辑实现类
 * </pre>
 */
@Service
@Slf4j
public class HouseServiceImpl implements HouseService {


    @Autowired
    private HouseMapper houseMapper;

    @Override
    public Page<House> findHouseByCondition(House condition, Page<House> page) {
        List<House> houseList = houseMapper.findHouseByCondition(condition, page);
        return page.setRecords(houseList);
    }

    @Override
    public Page<House> findByRentUserId(Long userId, Page<House> page) {
        List<House> houseList = houseMapper.findByRentUserId(userId, page);
        return page.setRecords(houseList);
    }


    @Override
    public BaseMapper<House> getRepository() {
        return houseMapper;
    }

    @Override
    public House insert(House house) {
        houseMapper.insert(house);
        return house;
    }

    @Override
    public House update(House house) {
        houseMapper.updateById(house);
        return house;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long houseId) {
        houseMapper.deleteById(houseId);
    }

    @Override
    public QueryWrapper<House> getQueryWrapper(House house) {
        //对指定字段查询
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        if (house != null) {
            if (StrUtil.isNotBlank(house.getHouseTitle())) {
                queryWrapper.like("house_title", house.getHouseTitle());
            }
            if (StrUtil.isNotBlank(house.getHouseContent())) {
                queryWrapper.like("house_content", house.getHouseContent());
            }
            if (house.getHouseStatus() != null && house.getHouseStatus() != -1) {
                queryWrapper.eq("house_status", house.getHouseStatus());
            }
        }
        return queryWrapper;
    }

    @Override
    public House insertOrUpdate(House house) {
        if (house.getId() == null) {
            insert(house);
        } else {
            update(house);
        }
        return house;
    }


    @Override
    public List<House> getLatestHouse(Long cityId, int limit) {
        return houseMapper.getLatestHouse(cityId, limit);
    }

    @Override
    public Integer countByStatus(Integer houseStatus) {
        return houseMapper.countByStatus(houseStatus);
    }

    @Override
    public List<House> getUnionRentHouse(House house) {

        House temp = new House();
        temp.setNumber(house.getNumber());
        temp.setUserId(house.getUserId());
        temp.setHouseTitle(house.getHouseTitle());
        temp.setCityId(house.getCityId());
        if (temp.getNumber() != null && temp.getNumber().length() > 2) {
            if (temp.getNumber().indexOf("室") != -1) {
                temp.setNumber(temp.getNumber().substring(0, temp.getNumber().indexOf("室") + 1));
            }
        }
        return houseMapper.getUnionRentHouse(temp);
    }

}

